
<?php
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'id' => 'page-form',
    'type' => 'horizontal',
    'enableAjaxValidation' => false,
    'enableClientValidation' => true,
    'clientOptions' => array(
        'validateOnSubmit' => true
    )
        ));
?>

<p class="help-block">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<?php echo $form->textFieldRow($model, 'name', array('class' => 'span5', 'maxlength' => 255)); ?>

<?php echo $form->textFieldRow($model, 'alias', array('class' => 'span5', 'maxlength' => 255)); ?>

<?php echo $form->textFieldRow($model, 'title', array('class' => 'span5', 'maxlength' => 255)); ?>

<?php echo $form->textFieldRow($model, 'keywords', array('class' => 'span5', 'maxlength' => 255)); ?>

<?php echo $form->textAreaRow($model, 'description', array('class' => 'span5', 'maxlength' => 255)); ?>

<?php //echo $form->textFieldRow($model, 'layout', array('class' => 'span5', 'maxlength' => 45)); ?>

<?php echo $form->dropDownListRow($model, 'status', array(1 => 'Active', 0 => 'Deactive'), array('class' => 'span5')); ?>

<hr/>
<?php $index = 0 ?>
<div class="box">
    <header>
        <h5>Header blocks</h5>
    </header>
    <div class="body">
        <ul id="block-regions-1">
            <?php if(!$model->isNewRecord) : foreach($model->getBlockRegion(1) as $block) : $index++; ?>
            <li>
                <?php $this->renderPartial('_block', array('model' => $block, 'blocks' => Block::model()->findAll(), 'index' => $index)) ?>
            </li>
            <?php endforeach; endif; ?>
        </ul>
    </div>
</div>
<hr/>
<div class="box">
    <header>
        <h5>Content blocks</h5>
    </header>
    <div class="body">
        <ul id="block-regions-2">
            <?php if(!$model->isNewRecord) : foreach($model->getBlockRegion(2) as $block) : $index++; ?>
            <li>
                <?php $this->renderPartial('_block', array('model' => $block, 'blocks' => Block::model()->findAll(), 'index' => $index)) ?>
            </li>
            <?php endforeach; endif; ?>
        </ul>

    </div>
</div>
<hr/>
<div class="box">
    <header>
        <h5>Footer blocks</h5>
    </header>
    <div class="body">
        <ul id="block-regions-3">
            <?php if(!$model->isNewRecord) : foreach($model->getBlockRegion(3) as $block) : $index++; ?>
            <li>
                <?php $this->renderPartial('_block', array('model' => $block, 'blocks' => Block::model()->findAll(), 'index' => $index)) ?>
            </li>
            <?php endforeach; endif; ?>
        </ul>
    </div>
</div>

<hr/>
<div class="form-actions">
    <select id="blockRegion">
        <option value="1">Header</option>
        <option value="2">Content</option>
        <option value="3">Footer</option>
    </select>
    <input type="button" class="btn btn-groupn-" id="addBlock" value="Add block" />
</div>
<hr/>
<div class="form-actions">
    <?php
    $this->widget('bootstrap.widgets.TbButton', array(
        'buttonType' => 'submit',
        'type' => 'primary',
        'label' => $model->isNewRecord ? 'Create' : 'Save',
    ));
    ?>
</div>

<?php $this->endWidget(); ?>

<script>
$(document).ready(function(){
    var index = <?php echo $index+1 ?>;

    $("#addBlock").click(function(){
        var region = $("#blockRegion").val();

        $.ajax({
            url : "<?php echo url('/page/AddBlock') ?>",
            data : "index="+index+"&region="+region,
            success : function(response){
                $("#block-regions-"+region).append("<li>"+response+"</li>");

                index++;
            }
        });
    });
    $(".removeBlock").live("click", function(){
        $(this).closest("li").remove();
        return false;
    });
});
</script>